﻿Imports System.IO

Public Class frmEmployeeEntry

    Dim _PhotoPath As String = ""
    Public _isOtherForm As Boolean = False

    Private Sub frmEmployeeEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If lblID.Text <> "" Then
            getTextbox()
        Else
            reset()
        End If
    End Sub

    Protected Sub getTextbox()
        Dim dt As New DataTable
        Dim oEmployee As New cEmployee
        dt = oEmployee.select_Employee_byID(CInt(lblID.Text))

        txtEmpName.Text = dt.Rows(0)("EmployeeName")
        cboGender.Text = dt.Rows(0)("Gender")
        dtpDOB.Value = dt.Rows(0)("DOB")
        txtAddress.Text = dt.Rows(0)("Address")
        txtTel.Text = dt.Rows(0)("Tel")
        txtEmail.Text = dt.Rows(0)("Email")
        'For Image
        If dt.Rows(0)("Photo") IsNot DBNull.Value Then
            Dim pic As Byte()
            pic = dt.Rows(0)("Photo")
            Dim streamPhoto As New MemoryStream(pic, True)
            Dim bmp As Bitmap = New Bitmap(streamPhoto)
            Dim bmp1 As Bitmap = New Bitmap(bmp)
            picPhoto.Image = bmp1
            streamPhoto.Close()
        Else
            picPhoto.Image = My.Resources.Photo
        End If
        txtPosition.Text = dt.Rows(0)("Position")
    End Sub

    Private Sub ShowForm(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        Me.Close()
        frm.Show()
        frm.BringToFront()
    End Sub

    Protected Sub reset()
        txtEmpName.Clear()
        txtEmpName.Focus()
        If cboGender.Items.Count > 0 Then
            cboGender.SelectedIndex = 0
        Else
            cboGender.SelectedIndex = -1
        End If
        dtpDOB.Value = Format(#1/1/1980#, "dd/MM/yyyy")
        txtAddress.Clear()
        txtTel.Clear()
        txtEmail.Clear()
        picPhoto.Image = My.Resources.Photo
        txtPosition.Clear()
        _PhotoPath = ""
    End Sub

    Public Sub EnableControl()
        txtEmpName.Enabled = False
        cboGender.Enabled = False
        dtpDOB.Enabled = False
        txtAddress.Enabled = False
        txtTel.Enabled = False
        txtEmail.Enabled = False
        picPhoto.Enabled = False
        txtPosition.Enabled = False
        btnChoose.Enabled = False
        btnRemove.Enabled = False
        btnSave.Enabled = False
        btnList.Enabled = False
        btnClean.Enabled = False
        btnClose.Enabled = True
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _PhotoPath = OpenFileDialog.FileName
            picPhoto.ImageLocation = _PhotoPath
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        picPhoto.Image = My.Resources.Photo
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
        If Application.OpenForms().OfType(Of frmEmployee).Any Then
            frmEmployee.getEmployee()
            Close()
        Else
            ShowForm(frmEmployee)
        End If
    End Sub

    Protected Function CheckNull()
        Dim oc As New cControl
        If oc.checknull(txtEmpName.Text) And oc.checknull(cboGender.Text) And oc.checknull(dtpDOB.Value) And oc.checknull(txtAddress.Text) And oc.checknull(txtTel.Text) And oc.checknull(txtEmail.Text) And oc.checknull(txtPosition.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If lblID.Text <> "" Then
            updated()
        Else
            inserted()
        End If
    End Sub

    Protected Sub inserted()
        Dim oEmployee As New cEmployee
        If CheckNull() Then
            If oEmployee.insert_Employee(txtEmpName.Text, cboGender.Text, dtpDOB.Value, txtAddress.Text, txtTel.Text, txtEmail.Text, getPhoto(), txtPosition.Text) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                If _isOtherForm Then
                    frmUserEntry.getComboEmployee()
                    Close()
                End If
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub updated()
        Dim oEmployee As New cEmployee
        If CheckNull() Then
            If oEmployee.update_Employee(lblID.Text, txtEmpName.Text, cboGender.Text, dtpDOB.Value, txtAddress.Text, txtTel.Text, txtEmail.Text, getPhoto(), txtPosition.Text) Then
                MessageBox.Show("កែប្រែបានសម្រេច!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                frmEmployee.getEmployee()
                Close()
            Else
                MessageBox.Show("កែប្រែមិនបានសម្រេចទេ", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Function getPhoto() As Byte()
        Dim pic As Byte() = Nothing
        If _PhotoPath <> "" Then
            pic = File.ReadAllBytes(_PhotoPath)
        End If
        Return pic
    End Function

    Private Sub picPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picPhoto.Click
        btnChoose_Click(Nothing, Nothing)
    End Sub
End Class